﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.UI;
using Silvermoon.Windows.Styles;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Passwords.Models;
using Passwords.Biz;
using Passwords.Controls;
using Passwords.Utils;
using Silvermoon.OpenGL;

namespace Passwords.Pages
{
    public class TimePicker : ItemPickerPage
    {
        private Picker hourPicker, minutePicker;
        private TimePickerModel model;
        private AnimationGrid grid;

        public TimePicker()
            : base()
        {
            InitializePage();
        }



        private void InitializePage()
        {
            Style = StylePool.DarkEditWindow;
            SetTransition(300, TransitionMask.Fade | TransitionMask.Zoom, TransitionMask.Fade | TransitionMask.Explode);
            Title = "CHOOSE TIME";

            const PickerMode pm = PickerMode.Fade | PickerMode.AutoHide;
            Styles.SetClass<Picker>(new PickerStyle
            {
                ItemHeight = 140,
                Lense = Shape.Empty,
                //Lense = new BorderShape(3, PhoneColors.White),
                IsCyclic = true,
                Mode = pm,
                Margin = Margin.Zero,
                TextColor = PhoneColors.White
            });

            hourPicker = new Picker { Margin = Margin.Zero };
            minutePicker = new Picker { Margin = Margin.Zero };
            model = new TimePickerModel();

            grid = new AnimationGrid { Margin = Margin.Zero };

            ArrangeGrid();
            Controls.Add(grid);

        }

        protected override void OnShown()
        {
            base.OnShown();
            minutePicker.SelectedItemChanged += new EventHandler(OnSelectionChanged);
            hourPicker.SelectedItemChanged += new EventHandler(OnSelectionChanged);
        }

        void OnSelectionChanged(object sender, EventArgs e)
        {
            IsSaveEnabled = true;
        }

        private void ArrangeGrid()
        {
            GridDefinition def = new GridDefinition();
            def.Columns.AddRange(1f, 2f, 0.3f, 2f, 1f);
            def.Rows.AddRange(1f);
            def.CreateAutoCells();

            grid.Controls.Clear();
            grid.Definition = def;
            grid.AddControl(1, hourPicker);
            grid.AddControl(3, minutePicker);
            grid.AddControl(2, new Label { Text = ":", Font = FontCache.IndexFont, Margin = Margin.Centered, AutoSize = true });

            minutePicker.GetVirtualItem += OnGetPickerItem;
            hourPicker.GetVirtualItem += OnGetPickerItem;

        }

        protected override void OnPasswordItemChanged(Passwords.Biz.PasswordItem item)
        {
            base.OnPasswordItemChanged(item);
            if (item != null)
            {
                DateTime time = item.Value.StringToTime();
                minutePicker.ItemsSource = model.Minutes;
                hourPicker.ItemsSource = model.Hours;
                minutePicker.SelectedItemIndex = time.Minute;
                hourPicker.SelectedItemIndex = time.Hour;
                if (item.IsNew || string.IsNullOrEmpty(item.Value)) IsSaveEnabled = true;

            }
        }

        protected override void OnOK(object sender, CommandEventArgs e)
        {
            int h = hourPicker.SelectedItemIndex;
            int m = minutePicker.SelectedItemIndex;
            if (h >= 0 && m >= 0)
            {
                PasswordItem.Value = string.Format("{0:d2}:{1:d2}", h, m);
                base.OnOK(sender, e);
            }
        }

        void OnGetPickerItem(object sender, PickerItemEventArgs e)
        {
            var item = DatePickerItem.GetItem();
            item.Alignment = Alignment.Center;
            item.DataValue = e.Value;
            e.Container = item;
        }

    }
}
